//
//  TFDiscoverView.swift
//  weiboSina
//
//  Created by admin on 15/12/27.
//  Copyright © 2015年 admin. All rights reserved.
//

import UIKit
@IBDesignable
class TFDiscoverView: UIView {

 
    //右边的约束属性
    @IBOutlet weak var textFiledRightConstraint: NSLayoutConstraint!
    //文本
    @IBOutlet weak var textFiled: UITextField!
    //取消按钮
    @IBOutlet weak var cancleBtn: UIButton!
    //xib加载view
     class func searchBar() -> TFDiscoverView{
    
    return NSBundle.mainBundle().loadNibNamed("TFDiscover", owner: nil, options: nil).last! as! TFDiscoverView
        
        
    
    }
    
    
    
    
    
    
    override func awakeFromNib() {
        
        //添加文本框的放大镜
        let imageView = UIImageView()
        imageView.image = UIImage(named: "searchbar_textfield_search_icon")
        imageView.frame.size = CGSize(width: frame.height, height: frame.height)
//        imageView.sizeToFit()
        imageView.contentMode = .Center
        textFiled.leftView = imageView
        textFiled.leftViewMode = .Always
        
        
        //添加搜索框的边框和颜色，圆角
        textFiled.borderWidth = 1
        textFiled.borderColor = UIColor.orangeColor()
        textFiled.cornerRadius = 15
        
    }
    
    
    
    
    @IBAction func editDidBegining(sender: AnyObject) {
        
       self.textFiledRightConstraint.constant = self.cancleBtn.frame.width;
        
       UIView.animateWithDuration(0.25) { () -> Void in
        
        self.textFiled.layoutIfNeeded()
        }
      
    }
    
    
    @IBAction func clickCancleBtn(sender: AnyObject) {
        //键盘隐藏
        self.textFiled.endEditing(true)
       
        self.textFiledRightConstraint.constant = 0
        UIView.animateWithDuration(0.25) { () -> Void in
            
            self.textFiled.layoutIfNeeded()
        }
        
    }
}
